/*
 * Copyright 2010 Biz-e (info@biz-e.nl)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.site.client.editor;

import java.util.List;

import nl.biz_e.gwt.baselib.ui.client.widgets.GroupBox;
import nl.biz_e.gwt.baselib.ui.client.windows.MessageBox;
import nl.biz_e.gwt.baselib.ui.client.windows.MessageBox.MsgBoxButton;
import nl.biz_e.site.client.ContentService;
import nl.biz_e.site.client.ContentServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;

/**
 * TODO: Write type comment
 * 
 * @author Joost A. Bloemsma
 */
public class VersionsPanel extends EditorPanelBase {
    private VersionsTable versionsList;
    private ListBox keyList;
    private final ContentServiceAsync contentService;

    /**
     * Constructs a new VersionsPanel instance
     */
    public VersionsPanel() {
        super();
        contentService = GWT.create(ContentService.class);
        Grid grid = new Grid(1, 2);
        grid.setWidth("100%");
        HorizontalPanel hp = new HorizontalPanel();
        hp.setWidth("100%");
        GroupBox gbKey = new GroupBox(strings.key());
        gbKey.add(getKeyList());
        GroupBox gbVersion = new GroupBox(strings.version());
        gbVersion.add(getVersionList());
        hp.add(gbKey);
        hp.add(gbVersion);
        grid.setWidget(0, 0, hp);
        grid.setWidget(0, 1, new Button("click", new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                MessageBox mb = new MessageBox("Hello", "Test");
                mb.setHandler(new MessageBox.SimpleMessageBoxHandler() {

                    /**
                     * {@inheritDoc}
                     */
                    @Override
                    public void onClose(MsgBoxButton clickedButton) {
                        System.out.println(clickedButton.name());
                    }

                });
                mb.show();
            }
        }));
        this.add(grid);
    }

    private VersionsTable getVersionList() {
        if (this.versionsList == null) {
            this.versionsList = new VersionsTable(this);
        }
        return this.versionsList;
    }

    private ListBox getKeyList() {
        if (keyList == null) {
            keyList = new ListBox(false);

            contentService.getAvailableKeys(new AsyncCallback<List<String>>() {
                @Override
                public void onSuccess(List<String> result) {
                    for (String key : result) {
                        keyList.addItem(key);
                    }
                }

                @Override
                public void onFailure(Throwable caught) {
                    getAppManager().showMessage(bizeAEMessages.serverError());
                }
            });
            keyList.addChangeHandler(this);
        }
        return keyList;
    }

}
